package com.jhd.fuzhou.util;

import android.app.Activity;
import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;

import androidx.appcompat.app.AlertDialog;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.jhd.fuzhou.R;
import com.jhd.fuzhou.adapter.AddressAdapter;
import com.jhd.fuzhou.adapter.SupervisorAdapter;
import com.jhd.fuzhou.vo.SupervisedUnitVo;
import com.jhd.fuzhou.vo.SupervisionInspectionContentVo;
import com.jhd.fuzhou.vo.Supervisor;

import java.util.ArrayList;
import java.util.List;

public class DialogShow {
    public static void dialogShow(Context context, ImageView imageView, TextView textView,TextView tv_Supervisor_1, String title, List<Supervisor> supervisorList,List<SupervisedUnitVo> supervisedUnitVoList) {
        LayoutInflater mLayoutInflater = LayoutInflater.from(context);
        View view = mLayoutInflater.inflate(R.layout.dialog_person_choose, null);
        final androidx.appcompat.app.AlertDialog.Builder alertBuilder = new AlertDialog.Builder(context, R.style.ActionSheetDialogStyle);
        alertBuilder.setView(view);
        AlertDialog _alertDialog = alertBuilder.create();
        //获取当前Activity所在的窗体
        Window window = _alertDialog.getWindow();
        window.setGravity(Gravity.BOTTOM);
        _alertDialog.show();
        ImageView iv_dismiss = (ImageView) view.findViewById(R.id.iv_dismiss);
        TextView tv_title = (TextView) view.findViewById(R.id.tv_title);
        RecyclerView rv_choose = (RecyclerView) view.findViewById(R.id.rv_choose);
        LinearLayoutManager layoutManage = new LinearLayoutManager(context);
        rv_choose.setLayoutManager(layoutManage);
        tv_title.setText(title);//标题要改一下
        EditText et_search = (EditText) view.findViewById(R.id.et_search);
        ImageView iv_cancel = (ImageView) view.findViewById(R.id.iv_cancel);
        List<Supervisor> supervisorList2=new ArrayList<>();//用来展示的list
        List<SupervisedUnitVo> supervisedUnitVoList2=new ArrayList<>();//用来展示的list
        if(title.equals("监督员")){
            SupervisorAdapter recyclerAdapter=new SupervisorAdapter(context,textView,tv_Supervisor_1,_alertDialog,supervisorList);
            rv_choose.setAdapter(recyclerAdapter);
        }else if(title.equals("检查地址")){
            AddressAdapter recyclerAdapter=new AddressAdapter(context,textView,tv_Supervisor_1,_alertDialog,supervisedUnitVoList);
            rv_choose.setAdapter(recyclerAdapter);
        }
        et_search.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if(title.equals("监督员")){
                    supervisorList2.clear();
                    for (Supervisor supervisor : supervisorList) {
                        if(supervisor.getName().contains(s.toString())){
                            supervisorList2.add(supervisor);
                        }
                    }
                    SupervisorAdapter recyclerAdapter=new SupervisorAdapter(context,textView,tv_Supervisor_1,_alertDialog,supervisorList2);
                    rv_choose.setAdapter(recyclerAdapter);
                }else if(title.equals("检查地址")){
                    supervisedUnitVoList2.clear();
                    for (SupervisedUnitVo supervisedUnitVo : supervisedUnitVoList) {
                        if(supervisedUnitVo.getManagementAddress().contains(s.toString())){
                            supervisedUnitVoList2.add(supervisedUnitVo);
                        }
                    }
                    AddressAdapter recyclerAdapter=new AddressAdapter(context,textView,tv_Supervisor_1,_alertDialog,supervisedUnitVoList2);
                    rv_choose.setAdapter(recyclerAdapter);
                }

            }
        });
        iv_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                et_search.setText("");
                if (title.equals("监督员")) {
                    SupervisorAdapter recyclerAdapter = new SupervisorAdapter(context,textView,tv_Supervisor_1, _alertDialog, supervisorList);
                    rv_choose.setAdapter(recyclerAdapter);
                }else if(title.equals("检查地址")){
                    AddressAdapter recyclerAdapter=new AddressAdapter(context,textView,tv_Supervisor_1,_alertDialog,supervisedUnitVoList);
                    rv_choose.setAdapter(recyclerAdapter);
                }
            }
        });
        iv_dismiss.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                _alertDialog.dismiss();
            }
        });

        WindowManager.LayoutParams params = window.getAttributes() ;
        params.width =(int) (((Activity)context).getWindowManager().getDefaultDisplay().getWidth());                     //使用这种方式更改了dialog的框宽
        window.setAttributes(params);
    }

}
